/**
 * 折叠树
 */
function menu_tree(){
	$('table.main_table tbody tr').addClass('treetr'); //消除奇偶行色
	$("#main_tbody tr").each(function(){
		$('.child-of-'+$(this).attr('id')).hide();//有子树的隐藏
		var padding_l = 30+$(this).children('td').children('input[type=checkbox]').attr('level')*20;
		$(this).children().attr("style","text-align:left; padding-left:"+padding_l+"px; font-size:15px;");
		if($('.child-of-'+$(this).attr('id')).length>0){//有孩子的父树加class
			$(this).addClass('parent');			
			$(this).children('td').prepend("<span class=\"collapsed\" style=\"padding-left:20px;\"></span>")
			.click(function(){
			var toggleClass=$(this).children('span').attr('class');
			if(toggleClass == 'collapsed'){		
				var chickid = $(this).parents('tr').attr('id');
				$('.child-of-'+chickid).fadeIn('fast');
				$(this).children('span').removeClass('collapsed');
				$(this).children('span').addClass('expanded');
			} else if(toggleClass == 'expanded'){
				var chk = $("input[type='checkbox']");
				var count = chk.length;
				var num = chk.index($(this).children('input'));
				var level_bottom = chk.eq(num).attr('level');
				for(var j = num+1;j<count;j++){
					var le = chk.eq(j).attr('level');
					if(eval(le)>eval(level_bottom)) 
					{
					  $('#main_tbody tr').eq(j).fadeOut('fast').children('td').children('span').removeClass('expanded');
					  $('#main_tbody tr').eq(j).children('td').children('span').addClass('collapsed');
					}
					else if(eval(le) == eval(level_bottom)) break;	
				}
				$(this).children('span').removeClass('expanded');
				$(this).children('span').addClass('collapsed');
			}	
				});
		}
	});
}

/**
 * 选择框
 */
function checknode(obj){
	var chk = $('input[type=checkbox]');
	var count = chk.length;
	var num = chk.index(obj);
	var level_top=level_bottom = chk.eq(num).attr('level');
	//选择后，之前的父树要主动勾选
	for(var i=num; i>0; i--){
		var le = chk.eq(i).attr('level');
		if(evel(le) < evel(level_top)){
			chk.eq(i).attr('ehecked',true);
			var level_top = level_top-1;
		}
	}
}